<?php

	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	
	
	//print_r(get_headers("http://mh2.hh.cn/display.asp?id=61930&page=2"));exit;
	//echo make_headers("http://mh2.hh.cn/display.asp?id=61930&page=2");exit;
	$session_url_get = "http://mh2.hh.cn/display.asp?id=61930&page=2";
	
	$action = $_GET['action'];
	$tid = $_GET['tid'];
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	//开始获取该ID下的图片
	$DB->build_query(array(
			'select' 	=> '*',
			'from'		=> 'hhphotos',
			'where'		=>	'uid=' . $tid,
	));
	$qid = $DB->exec_query();
	$pics = '';
	while ($row = $DB->fetch_row($qid)) {
		$nums = $row['count'];
// 		$pre_url = $row['url'];
// 		for($i=0;$i<$nums;$i++){
// 			$pics .= $pre_url . get_pre_nums(3,$i) . get_pre_nums(3,$i+1) . ".jpg\r\n";
// 			$pics .= $pre_url . get_pre_nums(3,$i) . get_pre_nums(3,$i+1) . ".png\r\n";
// 		}
		//生成URL
		$pre = $row['prez'];
		$urls = unserialize($row['url']);
		foreach ($urls as $vurl) {
			$pics .= $pre . $vurl . "\r\n";
		}
	}
	if($action == 'getpicurls'){
		$target_file_name = ' -i total.lst';
		$agent_str = ' --header="User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"';
		$referer_str = ' --header="Referer: http://www.hhcomic.com/hhpage/"';
// 		$cookie_str = ' --header="Cookie: ASPSESSIONIDCAQDDTTR=NFNOANNBKPAPKFGPBMLHPOGA; virtualwall=vsid=095425449d1c8b7e952561583119a7ae;"';
// 		$cookie_str = make_headers($session_url_get);
		$ext_attr = " -cx -nH --cut-dirs=5";
		echo '采集脚本<BR><textarea rows="6" cols="80" tabindex="2" class="txtarea">@ECHO OFF
ECHO 下载开始：
wget' . $target_file_name . $agent_str . $referer_str . $cookie_str . $ext_attr . '</textarea><BR>';
		echo '<textarea rows="20" cols="80" tabindex="2" class="txtarea">' . $pics . '</textarea>';
	}
	else if($action == 'getpicfiles'){
// 		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// 		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// 		header("Content-Type: text/plain; charset=UTF-8");
// 		header("Content-Disposition: attachment;filename=\"$tid.lst\"");
		echo '<html xmlns="http://www.w3.org/1999/xhtml">
	<head></head>
	<body>
		<table align="center">
			<tr>
				<td>
					<a href="./pics.php?action=getpicfilesbat&tid=' . $tid . '">下载bat</a>
				</td>
			</tr>
			<tr>
				<td>
					<a href="./pics.php?action=getpicfileslist&tid=' . $tid . '">下载list</a>
				</td>
			</tr>
		</table>
	</body>
</html>';
				
	}
	else if ($action == 'getpicfilesbat'){
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Content-Type: application/octet-stream; charset=UTF-8");
		header("Content-Disposition: attachment;filename=\"1.bat\"");
		echo '@ECHO OFF
ECHO 下载开始：
wget -i total.lst --header="User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)" --header="Referer: http://www.hhcomic.com/hhpage/" -cx -nH --cut-dirs=5';
	}
	else if ($action == 'getpicfileslist'){
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Content-Type: text/plain; charset=UTF-8");
		header("Content-Disposition: attachment;filename=\"total.lst\"");
		echo $pics;
	}
	
	
	
	
	
	
	
	//更新访问计数
	$result = $DB->build_and_exec_query(
			array(
				'select' 	=> 'scount',
				'from'		=> 'hhtags',
				'where'		=>	'id=' . $tid,
			)
	);
	
	$where = 'id= ' . $tid .'';
	$DB->do_update('hhtags',
		array(					  
		   'scount'				=> $result['scount'] + 1,
		 ),
		 $where
	);

	
	
	
	//获取HEADER头里的SESSION
	function make_headers($url){
// 		$cookie_str = ' --header="Cookie: ASPSESSIONIDCAQDDTTR=NFNOANNBKPAPKFGPBMLHPOGA; virtualwall=vsid=095425449d1c8b7e952561583119a7ae;"';
		$cookie_str = ' --header="Cookie: ';
		$headers = get_headers($url);
		foreach ($headers as $v) {
			if (strstr($v, "Set-Cookie: ")){
				if(strstr($v,"virtualwall")){
					$vsid = explode('Set-Cookie:',$v);
					$vsid = explode(' domain',$vsid[1]);
					$cookie_str .= $vsid[0];
					break;
				}
				$vsid = explode('Set-Cookie: ',$v);
				$vsid = explode(' path',$vsid[1]);
				$cookie_str .= $vsid[0];
			}
		}
		$cookie_str .= '"';
		return $cookie_str;
	}
	
?>